package io.renren.modules.workmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.workmanagement.dao.WorkAnswerDao;
import io.renren.modules.workmanagement.entity.WorkAnswerEntity;
import io.renren.modules.workmanagement.service.WorkAnswerService;
import io.renren.modules.workmanagement.vo.FillAnswerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("workAnswerService")
public class WorkAnswerServiceImpl extends ServiceImpl<WorkAnswerDao, WorkAnswerEntity> implements WorkAnswerService {
    @Autowired
    private SysUserService sysUserService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WorkAnswerEntity> page = this.page(
                new Query<WorkAnswerEntity>().getPage(params),
                new QueryWrapper<WorkAnswerEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fillAnswer(FillAnswerVO fillAnswerVO, Long userId) {
        SysUserEntity user = sysUserService.getById(userId);
        this.removeByQuId(fillAnswerVO.getQuId());
        List<WorkAnswerEntity> collect = Arrays.stream(fillAnswerVO.getAnswers()).map(answer -> {
            WorkAnswerEntity workAnswerEntity = new WorkAnswerEntity();
            workAnswerEntity.setWorkId(fillAnswerVO.getWorkId());
            workAnswerEntity.setQuId(fillAnswerVO.getQuId());
            workAnswerEntity.setAnswer(answer);
            workAnswerEntity.setStudentId(userId);
            return workAnswerEntity;
        }).collect(Collectors.toList());

        this.saveOrUpdateBatch(collect);

    }

    @Override
    public void removeByQuId(Long quId) {
        this.lambdaUpdate().eq(WorkAnswerEntity::getQuId, quId).remove();
    }

    @Override
    public List<WorkAnswerEntity> getByWorkIdAndUserId(Long workId, Long userId) {
        return this.lambdaQuery().eq(WorkAnswerEntity::getWorkId,workId).eq(WorkAnswerEntity::getStudentId,userId).list();
    }

    @Override
    public List<WorkAnswerEntity> getByWorkIdAndUserIdAndQuId(Long workId, Long quId, Long userId) {
        return this.lambdaQuery()
                .eq(WorkAnswerEntity::getWorkId, workId)
                .eq(WorkAnswerEntity::getQuId, quId)
                .eq(WorkAnswerEntity::getStudentId, userId)
                .list();
    }

    @Override
    public List<WorkAnswerEntity> getByQuIds(List<Long> quIds) {
        return this.lambdaQuery().in(WorkAnswerEntity::getQuId, quIds).list();
    }

}
